<template>
  <p-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <p-spin :spinning="confirmLoading">
      <p-form :form="form">
        <p-row>
          <p-col :span="24">
            <p-form-item label="产品编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <f-popup
                contextPath="basic-business"
                v-decorator="['mcode', validatorRules.mcode]"
                :trigger-change="true"
                org-fields="mid,mcode,mname,mexplain,sprc,char5g"
                dest-fields="mid,mcode,mname,mexplain,sprc,explain,sn_field_type5g"
                code="wms_cpinfo"
                @callback="popupCallback"
              />
            </p-form-item>
          </p-col>
          <p-col :span="24">
            <p-form-item label="产品名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input
                disabled
                v-decorator="[ 'mname', validatorRules.mname]"
              ></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="24">
            <p-form-item label="产品描述" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input
                disabled
                v-decorator="[ 'mexplain', validatorRules.mexplain]"
              ></p-input>      
            </p-form-item>
          </p-col>


          <p-col :span="24">
            <p-form-item label="字段名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-select 
                v-decorator="[ 'filedName', validatorRules.filedName]"
              >
                <p-select-option
                  :value="item.filedName"
                  v-for="(item, index) in dynamicFieldList"
                  :key="index"
                >
                  {{ item.filedName }}
                </p-select-option>
              </p-select>              
            </p-form-item>
          </p-col>

          <p-col :span="24">
            <p-form-item label="字段字符" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input
                v-decorator="[ 'filedCharacter', validatorRules.filedCharacter]"
                placeholder="请输入字段字符"
              ></p-input>

            </p-form-item>
          </p-col>
          <p-col :span="24">
            <p-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'remark', validatorRules.remark]" placeholder="请输入备注"></p-input>
            </p-form-item>
          </p-col>
        </p-row>
      </p-form>
    </p-spin>
  </p-modal>
</template>

<script>
import { httpAction, getAction } from "@/api/manage";
import { SubmitOnchangMixin } from "@/mixins/SubmitOnchangMixin";
import pick from "lodash.pick";

export default {
  name: "MesBc5gSnModal",
  mixins: [SubmitOnchangMixin],
  components: {},
  data() {
    return {
      form: this.$form.createForm(this),
      title: "操作",
      width: 800,
      visible: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },

      confirmLoading: false,
      validatorRules: {
        mcode: { rules: [{ required: true, message: "请输入产品编码!" }] },
        mname: { rules: [{ required: true, message: "请输入产品名称!" }] },
        mexplain: { rules: [{ required: true, message: "请输入产品描述!" }] },
        filedName: { rules: [{ required: true, message: "请输入字段名称!" }],},
        filedCharacter: { rules: [{ required: true, message: "请输入字段字符!" }] },
      },
      dynamicFieldList: [],	
      url: {
        add: "/mes-code/mesBcFieldDynamic",
        edit: "/mes-code/mesBcFieldDynamic",
        dynamicField: '/mes-code/mesBcField/getDynamicField', // 获取动态字符型字段列表
        
      },
    };
  },
  created() {
  },
  methods: {
    add() {
      this.edit({});
      this.getDynamicField()
    },
    edit(record) {
      console.log('record', record);
      
      this.form.resetFields();
      this.model = Object.assign({}, record);
      this.visible = true;
      this.$nextTick(() => {
        this.form.setFieldsValue(
          pick(
            this.model,
            'mcode',
            'mname',
            'mexplain',
            'filedName',
            'filedCharacter'
          )
        );
      });
    },   

    // 获取字段名称列表
    getDynamicField(){
      getAction(this.url.dynamicField).then(res => {
        if(res.success) {
          this.dynamicFieldList = res.data
        }
      })
    },

    close() {
      this.$emit("close");
      this.visible = false;
    },
    handleOk() {
      const that = this;
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true;
          let httpurl = "";
          let method = "";
          let formData = Object.assign(this.model, values);
          this.formData = Object.assign(this.model, values);

          this.dynamicFieldList.forEach( ele => {
            if(formData.filedName == ele.filedName) {

              formData.mesBcFieldId = ele.id
              this.formData.mesBcFieldId = ele.id
              formData.fieldInfo = ele
              this.formData.fieldInfo = ele
              const item = JSON.parse(JSON.stringify(ele))
              delete item.id
              delete item.filedCharacter
              Object.keys(item).forEach( key => {
                formData[key] = item[key]
                this.formData[key] = item[key]
              })

            }
          } )


          //新增触发
          if (!this.model.id) {
            httpurl += this.url.add;
            method = "post";
            if (this.EnhanceJS.afterAdd) {
              //新增之前
              let Obj = eval(this.EnhanceJS.afterAdd);
              new Obj(this);
            } else {
              console.log("表单提交数据", formData);
              that.modalFrom(httpurl, formData, method);
            }
          } else {
            //编辑触发
            httpurl += this.url.edit;
            method = "put";
            if (this.EnhanceJS.afterEdit) {
              //编辑之前
              let Obj = eval(this.EnhanceJS.afterEdit);
              new Obj(this);
            } else {
              console.log("表单提交数据", formData);
              that.modalFrom(httpurl, formData, method);
            }
          }
        }
      });
    },
    handleCancel() {
      this.close();
    },
    popupCallback(row) {

      this.form.setFieldsValue(
        pick(
          row,
          'mname',
          "mcode",
          "snFieldType5g",
          "mexplain"
        )
      );
    },

		handleChange(value) {
			console.log(`selected ${value}`);
		},
		handleBlur() {
			console.log('blur');
		},
		handleFocus() {
			console.log('focus');
		},
		filterOption(input, option) {
			return (
				option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
			);
		},

  },
};
</script>
